<?php
include './c/config.php';
include './session/mysql.sessions.php';
$session = new Session();

$zslink='';
if (isset($_GET['zslink'])){
    $zslink=$_GET['zslink'];
}
unset($_SESSION['openid']);
$_SESSION['rand']=rand(1,9);
if (isset($_GET['op'])){
    $openid=$_GET['op'];
    $_SESSION['openid']=$openid;
}
if (isset($_GET['friend'])){
    $openidfriend=$_GET['friend'];
    $_SESSION['friendOp']=$openidfriend;
}
if (isset($_GET['f']) && empty($_SESSION['friendOp'])){
    unset($_SESSION['openid']);
}
if (isset($_SESSION['openid'])){
   
    
    ini_set("display_errors", "On");
    error_reporting(E_ALL | E_STRICT);
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    $openid=$_SESSION['openid'];
    $sql="select phone,nickname,headimg from ".$tableVer."_users where openid='".$openid."'  limit 0,1 ";
    $query = $mysqli->query($sql);
    $getUserInfo=$query->fetch_assoc();
    $avatar=$getUserInfo['headimg'];
    $nickname=base64_decode($getUserInfo['nickname']);
    $phone=$getUserInfo['phone'];
    
    $userFen=0;
    $queryFen=$mysqli->query("select fen from ".$tableVer."_fen where openid='".$_SESSION['openid']."' limit 0, 1");
    if($queryFen->num_rows>0){
        $getFenInfo=$queryFen->fetch_assoc();
        $userFen=$getFenInfo['fen'];
    }
     
    $needAddZhandui='display:none;';
    $fTeamName='';
    if (isset($_SESSION['friendOp']) && !empty($_SESSION['friendOp']) && $_SESSION['friendOp'] != $_SESSION['openid']){
        $needAddZhandui='display:block;';
        $queryHasTeam=$mysqli->query("SELECT id FROM ".$tableVer."_group where openid='".$_SESSION['openid']."' limit 0, 1");
        if($queryHasTeam->num_rows>0){
            $needAddZhandui='display:none;';
        }else{
            $queryFriendTeam=$mysqli->query("SELECT tid FROM ".$tableVer."_group where openid='".$_SESSION['friendOp']."' limit 0, 1");
            if($queryFriendTeam->num_rows>0){
                
                $getFTId= $queryFriendTeam->fetch_assoc();
                $queryTeam=$mysqli->query("SELECT name FROM ".$tableVer."_team where id='".$getFTId['tid']."' limit 0, 1");
                if($queryTeam->num_rows>0){
                    $getFteam=$queryTeam->fetch_assoc();
                    $fTeamName=$getFteam['name'];
                    
                    $joinGroupInfo=array();
                    $queryGroupTeam=$mysqli->query("SELECT headimg,nickname FROM ".$tableVer."_group where tid='".$getFTId['tid']."' limit 0, 3");
                    while ($list=$queryGroupTeam->fetch_array()){
                        $joinGroupInfo[]=array('a'=>$list['headimg'],'n'=>base64_decode($list['nickname']));
                    }
                    if (count($joinGroupInfo) > 2){
                        $needAddZhandui='display:none;';
                    }
                }
            }
        }  
    }
    
    $gameStatus=1; //游戏正常
    $query=$mysqli->query("SELECT shuoming,sharetitle,sharedesc,shareimg,starttime,endtime,game_bg FROM z_game where is_open='1' limit 0, 1");
    $sharetitle=$sharedesc='';
    $shareimg='http://climb-game.51wom.com/images/64.jpg';
    $game_bg='images/bg.jpg';
    $ggboxtime='';
    $shuoming='';
    if($query->num_rows>0){
        $getGame=$query->fetch_assoc();
        $sharetitle=$getGame['sharetitle'];
        $sharedesc=$getGame['sharedesc'];
        $shuoming=stripcslashes($getGame['shuoming']);
        $shareimg='http://climb-game.51wom.com/app/data/upload/'.$getGame['shareimg'];
        if (!empty($getGame['game_bg'])){
            $game_bg='app/data/upload/'.$getGame['game_bg'];
        }
        
        $tmpStart=explode(' ', $getGame['starttime']);
        if (isset($tmpStart[0])){
            $tmpStartB=explode('-', $tmpStart[0]);
            $tmpEnd=explode(' ', $getGame['endtime']);
            $tmpEndB=explode('-', $tmpEnd[0]);
            $ggboxtime=$tmpStartB[0].'年'.$tmpStartB[1].'月'.$tmpStartB[2].'日--'.$tmpEndB[0].'年'.$tmpEndB[1].'月'.$tmpEndB[2].'日';
            
            $tmpStart=$tmpStartB=NULL;
        }
        
        
        
        $gameEndTime=strtotime($getGame['endtime']);
        $gameStartTime=strtotime($getGame['starttime']);
        $timeNow=time();
        if ($timeNow < $gameStartTime){
            $gameStatus=2; //还没开始
        }
        if ($timeNow > $gameEndTime){
            $gameStatus=3; //结束
        }
    }
    
    
    include 'jssdk.php';
    $jssdk = new JSSDK($appId, $secret);
    $signPackage = @$jssdk->GetSignPackage();
 
    include 'index-html.php';
}else{
    $_SESSION['friendOp']='';
    if (isset($_SESSION['rand'])){
        header("Content-Type:text/html;charset=UTF-8");
        include './c/config.php';
        $f='';
        if (isset($_GET['f'])){
            $f=$_GET['f'];
            $_SESSION['friendOp']=$f;
        }
        $redirect_uri = urlencode('http://climb-game.51wom.com/b.php?f='.$f.'&zslink='.$zslink);
        $api_userinfo_base='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base#wechat_redirect';
        
        header('Location: '.$api_userinfo_base);
    }else{
        echo 'update your browser';
    }
}


?>